"""
@Author  : 康帅
@Time    : 2021/3/11 21:12
@Function: 线性数据结构：队列(先入先出FILO) 栈(先入后出FIFO)
"""


class MyQueue:

    def __init__(self, capacity):
        self.list = [None] * capacity
        # 队头
        self.front = 0
        # 队尾
        self.rear = 0

    def enqueue(self, element):
        """ 入栈 """

        # (队尾下标+1)%数组长度=队头下表，说明队列已经满了
        if (self.rear + 1) % len(self.list) == self.front:
            raise Exception("队列已经满了！！！")
        self.list[self.rear] = element
        self.rear = (self.rear + 1) % len(self.list)

    def dequeue(self):
        """ 出栈 """

        if self.rear == self.front:
            raise Exception("队列已经满了！！！")
        dequeue_element = self.list[self.front]
        self.front = (self.front + 1) % len(self.list)
        return dequeue_element

    def output(self):
        """ 输出 """

        i = self.front
        while i != self.rear:
            print(self.list[i])
            i = (i + 1) % len(self.list)


if __name__ == '__main__':
    my_queue = MyQueue(6)
    my_queue.enqueue(3)
    my_queue.enqueue(5)
    my_queue.enqueue(6)
    my_queue.dequeue()
    my_queue.dequeue()
    my_queue.enqueue(2)
    my_queue.enqueue(4)
    my_queue.output()
